var data;
var toolbar;
var dataBooks;
var newimgname;
var newimgname2;
var data2;
var data3;
var num = 1;


// --------------------------用户界面--------------------------------------
// 默认用户界面
$(function() {
    // 获得当前登录的用户名
    var usernameLogin1 = localStorage.getItem("LoginLocal");

    // 修改右上角是谁登录的
    $("#usernameLogin").html(usernameLogin1);

    var str = localStorage.getItem("users");
    usersLocal = JSON.parse(str);
    var usersLocalAdmin = JSON.parse(str);

    //当登录用户不是管理员的时候显示数据只显示自己本身的一条信息
    var usernames = [];
    ids = [];
    for (let i = 0; i < usersLocal.length; i++) {
        usernames.push(usersLocal[i].username);
        ids.push(usersLocal[i].id);
    }

    // 获得当前用户名的索引值
    nameindex = usernames.indexOf(usernameLogin1);
    // 获取当前用户的对象字符串
    var str111 = JSON.stringify(usersLocal[nameindex]);
    // 将当前用户的对象字符串转换为对象
    var jq = JSON.parse(str111);
    // 新建一个对象存储变量
    var jq1 = [{
        "id": jq.id,
        "username": jq.username,
        "password": jq.password,
        "phone": jq.phone
    }];

    var strjq1 = JSON.stringify(jq1);
    // 将这个用户对象存储在本地会话中
    localStorage.setItem("LocalUser", strjq1);
    // 拿到存储在本地会话的当前登录对象字符串
    var struserLocal = localStorage.getItem("LocalUser");
    // 将当前登录对象字符串转化为jQuery对象
    var usersLocalUserLocal = JSON.parse(struserLocal);

    // 修改导入数据表格模板的库文件

    if (nameindex != 0) {
        data = usersLocalUserLocal;
        toolbar = '#barDemo1';
    } else {
        data = usersLocalAdmin;
        toolbar = '#barDemo';
    }
    // 调用设置用户信息表格的函数导入数据
    setSj();

    //    删除全部按钮的点击事件
    $(".doDeleteAllBtn").click(function() {
        if (nameindex == 0) {
            localStorage.removeItem("users");
            location.reload();
        } else {
            //使用layer，加载layer模块
            layui.use('layer', function() {
                var layer = layui.layer;
                layer.open({
                    type: 1,
                    content: '<div style="text-align: center;margin-top:50px;">普通没有权限全部删除</div>',

                    //这里content是一个普通的String
                    area: ['200px', '200px'],
                    btn: ['确定', '取消'],
                    yes: function(index, layero) {
                        // 确定按钮
                        layer.closeAll();
                    },
                    btn2: function(index, layero) {
                        //按钮【按钮二】的回调

                    },
                    cancel: function() {
                        //右上角关闭回调
                        //return false 开启该代码可禁止点击该按钮关闭
                    }
                });
            });
        }
    });
});

// 导入用户的信息表格函数
function setSj() {
    layui.use('table', function() {
        var table = layui.table;
        table.render({
            elem: '#test',
            cols: [
                [{
                    field: 'id',
                    width: 300,
                    title: '用户ID',
                    sort: true
                }, {
                    field: 'username',
                    width: 250,
                    title: '用户名'
                }, {
                    field: 'password',
                    width: 270,
                    title: '密码'
                }, {
                    field: 'phone',
                    width: 400,
                    title: '手机号'
                }, {
                    field: 'cz',
                    width: 424,
                    title: '操作',
                    align: 'center',
                    toolbar: toolbar
                }]
            ],
            data: data
        });
    });
}

// 用户界面新建按钮的点击事件
function donewUser(obj) {
    if (nameindex == 0) {
        layui.use('layer', function() {
            var layer = layui.layer;
            layer.open({
                type: 1,
                content: '<div class="box1">' +
                    '<form>' +
                    '<div class="layui-form-item">' +
                    '<label class="layui-form-label">用户名</label>' +
                    '<div class="layui-input-block">' +
                    '<input type="text" name="newusername" id="newusername" required lay-verify="required|username" placeholder="请输入用户名" autocomplete="off" class="layui-input inputWidth">' +
                    '</div>' +
                    '</div>' +
                    '<div class="layui-form-item">' +
                    '<label class="layui-form-label">密码</label>' +
                    '<div class="layui-input-inline">' +
                    '<input type="password" name="newpassword" id="newpassword" required lay-verify="required|password" placeholder="请输入密码" autocomplete="off" class="layui-input inputWidth">' +
                    '</div>' +
                    '</div>' +
                    '<div class="layui-form-item">' +
                    '<label class="layui-form-label">手机号</label>' +
                    '<div class="layui-input-inline">' +
                    '<input type="text" name="newphone" id="newphone required lay-verify="required|phone" placeholder="请输入手机号" autocomplete="off" class="layui-input inputWidth">' +
                    '</div>' +
                    '</div>' +
                    '</form>' +
                    '</div>',
                //这里content是一个普通的String
                area: ['500px', '300px'],
                btn: ['新增', '取消'],
                yes: function(index, layero) {
                    // 新增按钮
                    var inputs = $(".box1").find("input");
                    var usernewData = [];
                    $.each(inputs, function(v, i) {
                            console.log(i.value);
                            usernewData[v] = i.value;
                        })
                        var usernames = [];
                        for (let i = 0; i < usersLocal.length; i++) {
                            usernames.push(usersLocal[i].username);
                        }
                        if(usernames.indexOf(usernewData[0])>=0){
                            alert("用户名已存在");
                        }else{
                            var usernewDatajq = {
                                "id": usersLocal.length + 1,
                                "username": usernewData[0],
                                "password": usernewData[1],
                                "phone": usernewData[2]
                            }
                            usersLocal[usersLocal.length] = usernewDatajq;
                            localStorage.setItem("users", JSON.stringify(usersLocal));
                            // location.reload();
                            setSj();
                            // location.reload();
                            layer.closeAll();
                        }



                },
                btn2: function(index, layero) {
                    //按钮【按钮二】的回调
                    console.log(2);
                    // return false;
                },
                cancel: function() {
                    //右上角关闭回调
                    console.log(4)
                        //return false 开启该代码可禁止点击该按钮关闭
                }
            });

        });
    } else {
        //使用layer，加载layer模块
        layui.use('layer', function() {
            var layer = layui.layer;
            layer.open({
                type: 1,
                content: '<div style="text-align: center;margin-top:50px;">普通没有权限新建</div>',

                //这里content是一个普通的String
                area: ['200px', '200px'],
                btn: ['确定', '取消'],
                yes: function(index, layero) {
                    // 新增按钮
                    console.log(1);
                    layer.closeAll();

                },
                btn2: function(index, layero) {
                    //按钮【按钮二】的回调
                    console.log(2);
                    // return false;
                },
                cancel: function() {
                    //右上角关闭回调
                    console.log(4)
                        //return false 开启该代码可禁止点击该按钮关闭
                }
            });

        });
    }
}

// 用户界面修改按钮的点击事件
function doOpen(obj) {
    var tr = $(obj).parents("tr");
    var tds = tr.find("td");
    var tdsArr = [];
    // 将表格中的数据存入数组
    $.each(tds, function(v, item) {
        tdsArr.push(item.innerText);
    });
    //使用layer，加载layer模块
    layui.use('layer', function() {
        var layer = layui.layer;
        layer.open({
            type: 1,
            content: '<div class="box1">' +
                '<form>' +
                '<div class="layui-form-item">' +
                '<label class="layui-form-label">用户名</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" name="newusername" id="newusername" readonly disabled value="' + tdsArr[1] + '" required lay-verify="required|username" placeholder="请输入用户名" autocomplete="off" class="layui-input inputWidth">' +
                '</div>' +
                '</div>' +
                '<div class="layui-form-item">' +
                '<label class="layui-form-label">密码</label>' +
                '<div class="layui-input-inline">' +
                '<input type="password" name="newpassword" id="newpassword" value="' + tdsArr[2] + '" required lay-verify="required|password" placeholder="请输入密码" autocomplete="off" class="layui-input inputWidth">' +
                '</div>' +
                '</div>' +
                '<div class="layui-form-item">' +
                '<label class="layui-form-label">手机号</label>' +
                '<div class="layui-input-inline">' +
                '<input type="text" name="newphone" id="newphone" value="' + tdsArr[3] + '" required lay-verify="required|phone" placeholder="请输入手机号" autocomplete="off" class="layui-input inputWidth">' +
                '</div>' +
                '</div>' +
                '</form>' +
                '</div>',
            //这里content是一个普通的String
            area: ['500px', '300px'],
            btn: ['修改', '取消'],
            yes: function(index, layero) {
                // 新增按钮
                var inputs = $(".box1").find("input");
                var inputsData = [];
                // 将弹出层的数据写入数组
                $.each(inputs, function(v, i) {
                    inputsData.push(i.value);
                });
                // 根据用户的姓名查找对应是表格中的哪一行
                var usernames = [];
                for (let i = 0; i < usersLocal.length; i++) {
                    usernames.push(usersLocal[i].username);
                }
                // 根据表格中的姓名在所有用户姓名的数组中查找索引值
                // 根据索引值对用户信息的jQery对象进行修改
                var nameindex = usernames.indexOf(tdsArr[1]);
                usersLocal[nameindex] = {
                        "id": nameindex + 1,
                        "username": inputsData[0],
                        "password": inputsData[1],
                        "phone": inputsData[2],
                    }
                    // 将修改后的用户信息重新存储到库中
                var xiugai = JSON.stringify(usersLocal);
                localStorage.removeItem("users");
                localStorage.setItem("users", xiugai);
                location.reload();

                layer.closeAll();
            },
            btn2: function(index, layero) {
                //按钮【按钮二】的回调
                console.log(2);
                // return false;
            },
            cancel: function() {
                //右上角关闭回调
                console.log(4)
                    //return false 开启该代码可禁止点击该按钮关闭
            }
        });

    });

}

// 用户界面删除按钮的点击事件
function doDelete(obj) {
    var tr = $(obj).parents("tr");
    var td2 = $(tr).find("td").eq(1).text();
    // 如果是管理员，不能删除
    if (td2 == "admin") {
        //使用layer，加载layer模块
        layui.use('layer', function() {
            var layer = layui.layer;
            layer.open({
                type: 1,
                content: '<div style="text-align: center;margin-top:50px;">管理员不能删除</div>',

                //这里content是一个普通的String
                area: ['200px', '200px'],
                btn: ['确定', '取消'],
                yes: function(index, layero) {
                    // 删除按钮
                    layer.closeAll();
                    console.log(1);

                },
                btn2: function(index, layero) {
                    //按钮【按钮二】的回调
                    console.log(2);
                    // return false;
                },
                cancel: function() {
                    //右上角关闭回调
                    console.log(4)
                        //return false 开启该代码可禁止点击该按钮关闭
                }
            });

        });
    } else {
        var usernames = [];
        for (let i = 0; i < usersLocal.length; i++) {
            usernames.push(usersLocal[i].username);
        }
        var td2Index = usernames.indexOf(td2);
        usersLocal[td2Index] = "";
        localStorage.setItem("users", JSON.stringify(usersLocal));
        setSj();
        location.reload();

    }
    //JavaScript代码区域
    layui.use('element', function(data) {
        var element = layui.element;
        element.init();

        //导航条点击监听
        element.on('nav(bigData)', function(elem) {
            console.log(elem);
        });
        //tab切换监听
        element.on('tab(test)', function(data) {
            console.log(data);
        });
    });
}

// 查找用户
function doSearch(obj) {
    console.log(obj);
    var strsc = $(".wy").find("input")[0].value;
    console.log(strsc);
    var usernames = [];
    for (let i = 0; i < usersLocal.length; i++) {
        usernames.push(usersLocal[i].username);
    }
    console.log(usernames);
    var nameindex = usernames.indexOf(strsc);
    var chazhao = JSON.stringify(usersLocal);
    var sousuo = JSON.parse(chazhao);
    console.log(sousuo[nameindex])
    var sousuojq = [{
        "id": sousuo[nameindex].id,
        "username": sousuo[nameindex].username,
        "password": sousuo[nameindex].password,
        "phone": sousuo[nameindex].phone
    }];
    var strsousuo = JSON.stringify(sousuojq);
    localStorage.setItem("SeachUser", strsousuo);
    data = JSON.parse(localStorage.getItem("SeachUser"));
    setSj();
    // location.reload();
}



// ------------------------图书添加------------------------------------------
// 图书添加的图书导入
function setBookSj() {
    var usernameLogin1 = localStorage.getItem("LoginLocal");
    // 拿到存储在本地会话的添加图书字符串
    var straddBooks = localStorage.getItem("addBooks");
    // console.log(straddBooks);
    // 将添加图书字符串转化为jQuery对象
    var addBooksjq = JSON.parse(straddBooks);
    // console.log(addBooksjq);
    // 将对象中的图片地址存入数组中

    var addBooksArr1 = [];
    // 拿到存入会话的图片名称
    $.each(addBooksjq, function(v, i) {
        // console.log(i.img);
        addBooksArr1.push(i.img);
    });
    // console.log(addBooksArr1);
    if (nameindex != 0) {
        dataBooks = addBooksjq;
        toolbar = '#barDemo1';
    } else {
        dataBooks = addBooksjq;
        toolbar = '#barDemo';
    }
    if (usernameLogin1 == "admin") { //如果是admin登录
        layui.use('table', function() {
            var table = layui.table;
            table.render({
                elem: '#test2',
                cols: [
                    [{
                            field: 'id',
                            width: 175,
                            title: '图书编号',
                            sort: true
                        }, {
                            field: 'title',
                            width: 250,
                            title: '图书名称'
                        }, {
                            field: 'author',
                            width: 270,
                            title: '作者'
                        }, {
                            field: 'publishing',
                            width: 300,
                            title: '出版社'
                        },
                        {
                            field: 'img',
                            width: 224,
                            height: 200,
                            title: '图片',
                            templet: function(d) {
                                return '<div><img  src="imgs/addBooks/' + d.img + '"></div>'
                            }
                        }, {
                            field: 'cz',
                            width: 424,
                            title: '操作',
                            align: 'center',
                            toolbar: toolbar
                        }
                    ]
                ],
                data: dataBooks
            });
        });
    } else {
        layui.use('table', function() {
            var table = layui.table;
            table.render({
                elem: '#test2',
                cols: [
                    [{
                            field: 'id',
                            width: 200,
                            title: '图书编号',
                            sort: true
                        }, {
                            field: 'title',
                            width: 340,
                            title: '图书名称'
                        }, {
                            field: 'author',
                            width: 280,
                            title: '作者'
                        }, {
                            field: 'publishing',
                            width: 400,
                            title: '出版社'
                        },
                        {
                            field: 'img',
                            width: 424,
                            height: 500,
                            title: '图片',
                            templet: function(d) {
                                return '<div><img  src="../imgs/addBooks/' + d.img + '"></div>'
                            }
                        }
                    ]
                ],
                data: dataBooks
            });
        });
    }

}

// 图书的新建按钮
function donewBooks() {
    // 新建图书
    layui.use('layer', function() {
        var layer = layui.layer;

        layer.open({
            type: 1,
            content: '<div class="boxupimg">' +
                '<form>' +
                '<div class="layui-form-item">' +
                '<label class="layui-form-label">图书名称</label>' +
                '<div class="layui-input-inline">' +
                '<input type="text" name="bookname" id="bookname" lay-verify="" placeholder="请输入图书名称" autocomplete="off" class="layui-input inputWidth">' +
                '</div>' +
                '</div>' +
                '<div class="layui-form-item">' +
                '<label class="layui-form-label">图书作者</label>' +
                '<div class="layui-input-inline">' +
                '<input type="text" name="author" id="author" lay-verify="" placeholder="请输入图书作者" autocomplete="off" class="layui-input inputWidth">' +
                '</div>' +
                '</div>' +
                '<div class="layui-form-item">' +
                '<label class="layui-form-label">图书出版社</label>' +
                '<div class="layui-input-inline">' +
                '<input type="text" name="bookpublishing" id="bookpublishing" lay-verify="" placeholder="请输入出版社" autocomplete="off" class="layui-input inputWidth">' +
                '</div>' +
                '</div>' +

                '<div class="layui-form-item">' +
                '<label class="layui-form-label">上传图片</label>' +
                '<div class="layui-input-inline">' +
                '<div class=" layui-upload ">' +
                // '<button type="button" class="layui-btn " id="upimg1" >上传图片</button>' +
                '<input type="file" name="upimg1DOM" id="upimg1DOM" style="display:none" onchange="dochangeImg(this)" lay-verify="" placeholder="请选择图片文件" autocomplete="off" class="layui-input inputWidth">' +
                '<label for="upimg1DOM" id="upimg1DOMlable" class="layui-form-label" >请选择图片文件</label>' +
                '<div class="layui-upload-list ">' +
                '</div>' +
                '</div>' +
                '<img src="" alt="" style="height:200px;" id="newImg2">' +
                '</div>' +
                '</div>' +
                '</form>' +
                '</div>',
            //这里content是一个普通的String
            area: ['500px', '600px'],
            btn: ['新增', '取消'],
            yes: function(index, layero) {

                // 新增按钮
                // console.log(1);
                // console.log($(".boxupimg").find("input"));
                var inputs = $(".boxupimg").find("input");
                var booksnewData = [];
                $.each(inputs, function(v, i) {
                    // console.log(i.value);
                    booksnewData[v] = i.value;
                })
                booksnewData[3] = newimgname;

                var addBooksstr = localStorage.getItem("addBooks");
                var addBooksjq = JSON.parse(addBooksstr);

                var newBook = {
                    "id": addBooksjq.length + 1,
                    "title": booksnewData[0],
                    "author": booksnewData[1],
                    "publishing": booksnewData[2],
                    "img": booksnewData[3]
                };

                addBooksjq.push(newBook);
                var addBooksstr2 = JSON.stringify(addBooksjq);
                localStorage.setItem("addBooks", addBooksstr2);
                setBookSj();
                layer.closeAll();

            },
            btn2: function(index, layero) {
                //按钮【按钮二】的回调
                console.log(2);
                // return false;
            },
            cancel: function() {
                //右上角关闭回调
                console.log(4)
                    //return false 开启该代码可禁止点击该按钮关闭
            }
        });
        layui.use('upload', function() {
            var $ = layui.jquery,
                upload = layui.upload;

            //普通图片上传
            var uploadInst = upload.render({
                elem: '#upimg1',
                url: 'https://httpbin.org/post' //改成您自己的上传接口
                    ,
                before: function(obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result) {
                        $('#upimg1demo1').attr('src', result); //图片链接（base64）
                    });
                },
                done: function(res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                },
                error: function() {
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722; ">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload ">重试</a>');
                    demoText.find('.demo-reload').on('click', function() {
                        uploadInst.upload();
                    });
                }
            });
        });
    });
}

//获得新建图片的地址
function dochangeImg(obj) {
    console.log(obj);
    newimgname = obj.files[0].name;
    console.log(newimgname);
    $("#newImg2").attr("src", "imgs/addBooks/" + newimgname);

}

//获得修改图片的地址
function dochangeImg2(obj) {
    num = 2;
    // console.log(obj);
    newimgname2 = obj.files[0].name;
    // console.log(newimgname);
    $("#newImg2").attr("src", "imgs/addBooks/" + newimgname2);
}

// 修改图书按钮
function doBooksChange(obj) {
    // console.log(obj);
    var tr = $(obj).parents("tr");
    var tds = tr.find("td");
    // console.log(tds);
    var tdsArr = [];
    // 将表格中的数据存入数组
    $.each(tds, function(v, item) {
        tdsArr.push(item.innerText);
    });
    var addBooksstr = localStorage.getItem("addBooks");
    var addBooksjq = JSON.parse(addBooksstr);
    var xiugaiIndex = addBooksjq[tdsArr[0] * 1 - 1];
    tdsArr[4] = xiugaiIndex.img;
    var oldimg = tdsArr[4];
    console.log("旧图片路径：" + oldimg);

    //使用layer，加载layer模块
    layui.use('layer', function() {
        var layer = layui.layer;
        layer.open({
            type: 1,
            content: '<div class="boxxiuimg">' +
                '<form>' +
                '<div class="layui-form-item">' +
                '<label class="layui-form-label">图书名称</label>' +
                '<div class="layui-input-inline">' +
                '<input type="text" name="bookname" id="bookname" lay-verify="" readonly placeholder="请输入图书名称" value=' + tdsArr[1] + ' autocomplete="off" class="layui-input inputWidth">' +
                '</div>' +
                '</div>' +
                '<div class="layui-form-item">' +
                '<label class="layui-form-label">图书作者</label>' +
                '<div class="layui-input-inline">' +
                '<input type="text" name="author" id="author" lay-verify="" placeholder="请输入图书作者"  value=' + tdsArr[2] + ' autocomplete="off" class="layui-input inputWidth">' +
                '</div>' +
                '</div>' +
                '<div class="layui-form-item">' +
                '<label class="layui-form-label">图书出版社</label>' +
                '<div class="layui-input-inline">' +
                '<input type="text" name="bookpublishing" id="bookpublishing" lay-verify="" placeholder="请输入出版社"  value=' + tdsArr[3] + ' autocomplete="off" class="layui-input inputWidth">' +
                '</div>' +
                '</div>' +

                '<div class="layui-form-item">' +
                '<label class="layui-form-label">上传图片</label>' +
                '<div class="layui-input-inline">' +
                '<div class=" layui-upload ">' +
                // '<button type="button" class="layui-btn " id="upimg1" >上传图片</button>' +
                '<input type="file" name="upimg1DOM" id="upimg1DOM" style="display:none" onchange="dochangeImg2(this)" lay-verify="" placeholder="请选择图片文件" autocomplete="off" class="layui-input inputWidth">' +
                '<label for="upimg1DOM" id="upimg1DOMlable" class="layui-form-label" >请选择图片文件</label>' +
                '<div class="layui-upload-list ">' +
                '</div>' +
                '</div>' +
                '<img src="../imgs/addBooks/' + tdsArr[4] + '" alt=""  id="newImg2" style="height:200px;">' +
                '</div>' +
                '</div>' +
                '</form>' +
                '</div>',
            //这里content是一个普通的String
            area: ['500px', '600px'],
            btn: ['修改', '取消'],
            yes: function(index, layero) {
                // 修改按钮
                // console.log(1);
                var inputs = $(".boxxiuimg").find("input");
                // console.log(inputs);
                var inputsData1 = [];
                // 将弹出层的数据写入数组
                $.each(inputs, function(v, i) {
                    // console.log(i.value);
                    inputsData1.push(i.value);
                });
                console.log("修改后的图片路径:" + newimgname2); //拿到修改后的图片路径
                console.log(tdsArr);
                for (var i = 1; i < tdsArr.length; i++) {
                    if (inputsData1[i] != tdsArr[i]) {
                        tdsArr[i] = inputsData1[i];
                    }
                    if (i == 4) {
                        console.log(num);
                        if (num == 1) {
                            // tdsArr[4]=newimgname2;
                            console.log(oldimg);
                            inputsData1[3] = oldimg;

                        }
                        if (num == 2) {
                            tdsArr[4] = newimgname2;
                            console.log(newimgname2);
                            inputsData1[3] = newimgname2;
                            num = 1;
                        }

                    }
                }
                console.log(inputsData1);
                var addBooksstr = localStorage.getItem("addBooks");
                var addBooksjq = JSON.parse(addBooksstr);
                var booksnames = [];
                $.each(addBooksjq, function(v, i) {
                        var bookname = i.title;
                        booksnames.push(bookname);
                    })
                    // console.log(booksnames);//表格中所有的书籍名称
                var xiugaibookIndex = booksnames.indexOf(inputsData1[0]);
                // console.log(xiugaibookIndex);
                var xiugaiBookjq = {
                    "id": tdsArr[0] * 1,
                    "title": inputsData1[0],
                    "author": inputsData1[1],
                    "publishing": inputsData1[2],
                    "img": inputsData1[3]
                };

                addBooksjq.splice(xiugaibookIndex, 1, xiugaiBookjq);
                console.log(addBooksjq);
                var addBooksstr2 = JSON.stringify(addBooksjq);
                localStorage.setItem("addBooks", addBooksstr2);
                setBookSj();
                layer.closeAll();
            },
            btn2: function(index, layero) {
                //按钮【按钮二】的回调
                console.log(2);
                // return false;
            },
            cancel: function() {
                //右上角关闭回调
                console.log(4)
                    //return false 开启该代码可禁止点击该按钮关闭
            }
        });

    });

}

//删除图书按钮
function doBooksDelete(obj) {
    var tr = $(obj).parents("tr");
    var td1 = $(tr).find("td").eq(0).text() * 1;

    var addBooksstr = localStorage.getItem("addBooks");
    var addBooksjq = JSON.parse(addBooksstr);
    var booksnames = [];
    var booksId = [];
    $.each(addBooksjq, function(v, i) {
        var bookname = i.title;
        var bookid = i.id;
        // console.log(i);
        booksnames.push(bookname);
        booksId.push(bookid);
    })
    console.log(booksnames); //表格中所有的书籍名称
    console.log(booksId); //表格中所有的书籍id
    var booksIdIndex = booksId.indexOf(td1);
    addBooksjq[booksIdIndex] = "";
    localStorage.setItem("addBooks", JSON.stringify(addBooksjq));
    // location.reload();
    setBookSj();
}

// 添加图书搜索导入
function setsearchSj() {
    layui.use('table', function() {
        var table = layui.table;
        table.render({
            elem: '#test2',
            cols: [
                [{
                        field: 'id',
                        width: 175,
                        title: '图书编号',
                        sort: true
                    }, {
                        field: 'title',
                        width: 250,
                        title: '图书名称'
                    }, {
                        field: 'author',
                        width: 270,
                        title: '作者'
                    }, {
                        field: 'publishing',
                        width: 300,
                        title: '出版社'
                    },
                    {
                        field: 'img',
                        width: 224,
                        height: 200,
                        title: '图片',
                        templet: function(d) {
                            return '<div><img  src="imgs/addBooks/' + d.img + '"></div>'
                        }
                    }, {
                        field: 'cz',
                        width: 424,
                        title: '操作',
                        align: 'center',
                        toolbar: toolbar
                    }
                ]
            ],
            data: data2
        });
    });
}

// 添加图书的查找图书
function doSearchBooksAdd(obj) {
    console.log(obj);
    var strsc = $(".wy").find("input")[0].value; //获取输入框中的书名
    console.log(strsc);
    var books = [];
    var straddBooks = localStorage.getItem("addBooks");
    var addBooksjq = JSON.parse(straddBooks);


    for (let i = 0; i < addBooksjq.length; i++) {
        books.push(addBooksjq[i].title);
    }
    console.log(books);
    var selectid = [];
    $.each(books, function(v, i) {
        // console.log(i);
        if (i == strsc) {
            // console.log(v);//拿到索引位置
            selectid.push(v);
        }
    })
    console.log(selectid);
    var newSearchBooks = [];
    for (var i = 0; i < selectid.length; i++) {
        console.log(addBooksjq[selectid[i]]);
        searchbookjq = addBooksjq[selectid[i]];
        newSearchBooks.push(searchbookjq);
    }
    console.log(newSearchBooks);
    var newSearchBooksstr = JSON.stringify(newSearchBooks);
    localStorage.setItem("SearchBooks", newSearchBooksstr);
    data2 = JSON.parse(localStorage.getItem("SearchBooks"));
    setsearchSj();

}


// ---------------------------图书查询------------------------------------------
// 图书查询的图书导入
function setBookCXSj() {
    // var addBooksURL = ["selected01.jpg", "selected02.jpg", "selected03.jpg", "selected04.jpg", "selected05.jpg"];
    layui.use('table', function() {
        var table = layui.table;
        table.render({
            elem: '#test3',
            cols: [
                [{
                        field: 'title',
                        width: 350,
                        title: '名称'
                    }, {
                        field: 'author',
                        width: 380,
                        title: '作者'
                    }, {
                        field: 'publishing',
                        width: 498,
                        title: '出版社'
                    },
                    {
                        field: 'img',
                        width: 400,
                        height: 200,
                        title: '图片',
                        templet: function(d) {
                            return '<div><img  src="imgs/guessLikes/' + d.img + '"></div>'
                                // console.log(d.img);
                        }
                    },
                ]
            ],
            data: dataguessLikesBooks
        });

    });
}

// 图书查询的搜索数据导入
function doSBooks() {
    layui.use('table', function() {
        var table = layui.table;
        table.render({
            elem: '#test3',
            cols: [
                [{
                        field: 'title',
                        width: 350,
                        title: '名称'
                    }, {
                        field: 'author',
                        width: 380,
                        title: '作者'
                    }, {
                        field: 'publishing',
                        width: 498,
                        title: '出版社'
                    },
                    {
                        field: 'img',
                        width: 400,
                        height: 200,
                        title: '图片',
                        templet: function(d) {
                            return '<div ><img  src="../imgs/guessLikes/' + d.img + '" class="aaa"></div>'
                        }
                    },
                ]
            ],
            data: data3
        });

    });
}

// 图书查询的查找
function doSearchBooks(obj) {
    // console.log(obj);
    var strsc2 = $(".wy").find("input")[0].value; //获取输入框中的书名
    // console.log(strsc2);
    var books2 = [];
    var strguessLikes = localStorage.getItem("guessLikes");
    var guessLikesjq = JSON.parse(strguessLikes);


    for (let i = 0; i < guessLikesjq.length; i++) {
        books2.push(guessLikesjq[i].title);
    }
    // console.log(books2);
    var selectid2 = [];
    $.each(books2, function(v, i) {
            // console.log(i);
            if (i == strsc2) {
                // console.log(v);//拿到索引位置
                selectid2.push(v);
            }
        })
        // console.log(selectid2);
    var newSearchBooks2 = [];
    for (var i = 0; i < selectid2.length; i++) {
        // console.log(guessLikesjq[selectid2[i]]);
        var searchbookjq2 = guessLikesjq[selectid2[i]];
        newSearchBooks2.push(searchbookjq2);
    }

    var newSearchBooksstr2 = JSON.stringify(newSearchBooks2);
    localStorage.setItem("SearchBooks2", newSearchBooksstr2);
    data3 = JSON.parse(localStorage.getItem("SearchBooks2"));
    // console.log(data3);
    doSBooks();

    console.log($(".aaa")[0]);

}
// 轮播图的路径导入
function slidesDom() {
    var getSwiperImgstr = localStorage.getItem("slides");
    var getSwiperImgjq = JSON.parse(getSwiperImgstr);
    var imgs = $("img");
    $.each(imgs, function(v, i) {
        $(i).attr("src", "imgs/slides/" + getSwiperImgjq[v]);
    });
}

// -------------------------切换模板页--------------------------------------------------
// 点击后台用户
$(function() {
    layui.use('element', function() {
        var element = layui.element;
    });
    $(".yonghu").click(function() {
        console.log("houtai");
        $("#layui-body0").html("1");
        var data = {};
        layui.use('laytpl', function() {
            var laytpl = layui.laytpl;
            var getTpl = document.getElementById("demo1").innerHTML;
            // console.log(getTpl);
            var view = document.getElementById("layui-body0");
            // console.log(view);
            laytpl(getTpl).render(data, function(html) {
                // console.log(html);
                view.innerHTML = html;
                $(function() {
                    // 获得当前登录的用户名
                    var usernameLogin1 = localStorage.getItem("LoginLocal");
                    // console.log(usernameLogin);
                    // 修改右上角是谁登录的
                    $("#usernameLogin").html(usernameLogin1);

                    var str = localStorage.getItem("users");
                    usersLocal = JSON.parse(str);
                    var usersLocalAdmin = JSON.parse(str);
                    // console.log(str);
                    // console.log(usersLocal);
                    // console.log(usersLocalAdmin);

                    var usernames = [];
                    ids = [];
                    for (let i = 0; i < usersLocal.length; i++) {
                        usernames.push(usersLocal[i].username);
                        ids.push(usersLocal[i].id);
                    }
                    // console.log(usernames);
                    // console.log(ids);

                    // console.log(usernameLogin1);
                    // 获得当前用户名的索引值
                    nameindex = usernames.indexOf(usernameLogin1);
                    // console.log(nameindex);
                    // 获取当前用户的对象字符串
                    var str111 = JSON.stringify(usersLocal[nameindex]);
                    // 将当前用户的对象字符串转换为对象
                    var jq = JSON.parse(str111);
                    // 新建一个对象存储变量
                    var jq1 = [{
                        "id": jq.id,
                        "username": jq.username,
                        "password": jq.password,
                        "phone": jq.phone
                    }];
                    // console.log(jq1);
                    var strjq1 = JSON.stringify(jq1);
                    // console.log(strjq1);
                    // 将这个用户对象存储在本地会话中
                    localStorage.setItem("LocalUser", strjq1);
                    // 拿到存储在本地会话的当前登录对象字符串
                    var struserLocal = localStorage.getItem("LocalUser");
                    // 将当前登录对象字符串转化为jQuery对象
                    var usersLocalUserLocal = JSON.parse(struserLocal);

                    // console.log(usersLocalUserLocal);
                    if (nameindex != 0) {
                        data = usersLocalUserLocal;
                        toolbar = '#barDemo1';
                    } else {
                        data = usersLocalAdmin;
                        toolbar = '#barDemo';
                    }

                    setSj();
                    // console.log($("#test")[0]);
                    //    删除全部
                    $(".doDeleteAllBtn").click(function() {
                        if (nameindex == 0) {
                            // console.log($("tbody tr"));
                            // console.log($("tbody tr").length);
                            localStorage.removeItem("users");
                            location.reload();
                        } else {
                            //使用layer，加载layer模块
                            layui.use('layer', function() {
                                var layer = layui.layer;
                                layer.open({
                                    type: 1,
                                    content: '<div style="text-align: center;margin-top:50px;">普通没有权限全部删除</div>',

                                    //这里content是一个普通的String
                                    area: ['200px', '200px'],
                                    btn: ['确定', '取消'],
                                    yes: function(index, layero) {
                                        // 确定按钮
                                        console.log(1);
                                        layer.closeAll();

                                    },
                                    btn2: function(index, layero) {
                                        //按钮【按钮二】的回调
                                        console.log(2);
                                        // return false;
                                    },
                                    cancel: function() {
                                        //右上角关闭回调
                                        console.log(4)
                                            //return false 开启该代码可禁止点击该按钮关闭
                                    }
                                });
                            });
                        }
                    });
                });
            });
        });
    });
});
// 点击图书添加
$(function() {
    layui.use('element', function() {
        var element = layui.element;
    });


    // 图书添加点击加载模板
    $(".booksAdd").click(function() {
        console.log("booksadd");
        $("#layui-body").html("");
        layui.use('laytpl', function() {
            var laytpl = layui.laytpl;
            var getTpl = document.getElementById("demo2").innerHTML;
            // console.log(getTpl);
            var view = document.getElementById("layui-body0");
            // console.log(view);
            laytpl(getTpl).render(data, function(html) {
                // console.log(html);
                view.innerHTML = html;
                var dataBooks;
                var toolimg;
                $(function() {
                    // 获得当前登录的用户名
                    var usernameLogin1 = localStorage.getItem("LoginLocal");
                    // console.log(usernameLogin);
                    // 修改右上角是谁登录的
                    $("#usernameLogin").html(usernameLogin1);

                    var str = localStorage.getItem("users");
                    usersLocal = JSON.parse(str);
                    var usersLocalAdmin = JSON.parse(str);
                    var usernames = [];
                    ids = [];
                    for (let i = 0; i < usersLocal.length; i++) {
                        usernames.push(usersLocal[i].username);
                        ids.push(usersLocal[i].id);
                    }

                    setBookSj();
                    //    删除全部
                    $(".doDeleteAllBtn").click(function() {
                        if (nameindex == 0) {
                            // console.log($("tbody tr"));
                            // console.log($("tbody tr").length);
                            localStorage.removeItem("addBooks");
                            location.reload();
                            setBookSj();
                        } else {
                            //使用layer，加载layer模块
                            layui.use('layer', function() {
                                var layer = layui.layer;
                                layer.open({
                                    type: 1,
                                    content: '<div style="text-align: center;margin-top:50px;">普通没有权限全部删除</div>',

                                    //这里content是一个普通的String
                                    area: ['200px', '200px'],
                                    btn: ['确定', '取消'],
                                    yes: function(index, layero) {
                                        // 确定按钮
                                        console.log(1);
                                        layer.closeAll();

                                    },
                                    btn2: function(index, layero) {
                                        //按钮【按钮二】的回调
                                        console.log(2);
                                        // return false;
                                    },
                                    cancel: function() {
                                        //右上角关闭回调
                                        console.log(4)
                                            //return false 开启该代码可禁止点击该按钮关闭
                                    }
                                });
                            });
                        }
                    });
                });

            });
        });
    });
});

// 点击图书查询
$(function() {

    layui.use('element', function() {
        var element = layui.element;
    });
    $(".booksSearch").click(function() {
        // console.log("booksSearch");
        $("#layui-body").html("");

        layui.use('laytpl', function() {
            var laytpl = layui.laytpl;
            var getTpl = document.getElementById("demo3").innerHTML;
            var view = document.getElementById("layui-body0");

            laytpl(getTpl).render(data, function(html) {
                // console.log(html);
                view.innerHTML = html;


                slidesDom();
                // 解析页面后进行轮播图的解析
                var mySwiper = new Swiper('.swiper-container', {
                    loop: true, // 循环模式选项
                    // autoplay: true,
                    autoplay:{
                        delay: 2000,
                        stopOnLastSlide: false,
                        disableOnInteraction: false,
                    },
                    // 如果需要分页器
                    pagination: {
                        el: '.swiper-pagination',
                        clickable: true,
                    },
                    // 如果需要前进后退按钮
                    navigation: {
                        nextEl: '.swiper-button-next',
                        prevEl: '.swiper-button-prev',
                    },
                })
                var dataBooks;
                var toolimg;
                $(function() {
                    // 拿到存储在本地会话的guessLikes字符串
                    var strguessLikes = localStorage.getItem("guessLikes");
                    // console.log(straddBooks);
                    // 将添加图书字符串转化为jQuery对象
                    var guessLikesjq = JSON.parse(strguessLikes);
                    // console.log(guessLikesjq);
                    // 将对象中的图片地址存入数组中
                    // var guessLikesURL = ["selected01.jpg", "selected02.jpg", "selected03.jpg", "selected04.jpg", "selected05.jpg"];
                    var guessLikesArr1 = [];
                    // 拿到存入会话的图片名称
                    $.each(guessLikesjq, function(v, i) {
                        // console.log(i.img);
                        guessLikesArr1.push(i.img);
                    });
                    // console.log(guessLikesArr1);
                    if (nameindex != 0) {
                        dataguessLikesBooks = guessLikesjq;
                        toolbar = '#barDemo1';
                    } else {
                        dataguessLikesBooks = guessLikesjq;
                        toolbar = '#barDemo';
                    }
                    setBookCXSj();
                    //    删除全部
                    $(".doDeleteAllBtn").click(function() {
                        if (nameindex == 0) {
                            // console.log($("tbody tr"));
                            // console.log($("tbody tr").length);
                            localStorage.removeItem("guessLikes");
                            location.reload();
                        } else {
                            //使用layer，加载layer模块
                            layui.use('layer', function() {
                                var layer = layui.layer;
                                layer.open({
                                    type: 1,
                                    content: '<div style="text-align: center;margin-top:50px;">普通没有权限全部删除</div>',

                                    //这里content是一个普通的String
                                    area: ['200px', '200px'],
                                    btn: ['确定', '取消'],
                                    yes: function(index, layero) {
                                        // 确定按钮
                                        console.log(1);
                                        layer.closeAll();

                                    },
                                    btn2: function(index, layero) {
                                        //按钮【按钮二】的回调
                                        console.log(2);
                                        // return false;
                                    },
                                    cancel: function() {
                                        //右上角关闭回调
                                        console.log(4)
                                            //return false 开启该代码可禁止点击该按钮关闭
                                    }
                                });
                            });
                        }
                    });
                });



            });
        });
    });
});

// 点击热销书展示
$(function() {

    layui.use('element', function() {
        var element = layui.element;
    });
    layui.use('element', function() {
        var element = layui.element;
    });
    $(".booksShow").click(function() {
        console.log("booksShow1");
        $("#layui-body0").html("");
        var pages = 1; //默认是第一页
        layui.use('laytpl', function() {
            var laytpl = layui.laytpl;
            var data = {};
            var getTpl = document.getElementById("demo4").innerHTML;
            // console.log(getTpl);
            var view = document.getElementById("layui-body0");
            // console.log(view);
            laytpl(getTpl).render(data, function(html) {
                // console.log(html);
                view.innerHTML = html;
                var booksstr = localStorage.getItem("books");
                // console.log(booksstr);
                var booksjq = JSON.parse(booksstr);
                // console.log(booksjq);
                // 描述
                var texts_12 = [];
                // 团购价
                var tgj_12 = [];
                // 原价
                var yj_12 = [];
                $.each(booksjq, function(v, i) {
                    // console.log(i.desc);
                    texts_12.push(i.desc);
                    tgj_12.push(i.newPrice);
                    yj_12.push(i.oldPrice);
                });
                // console.log(texts_12);
                // console.log(texts_12[0]);
                // $(".text_1").html(texts_12[0]);
                // $(".text_2").html(texts_12[1]);
                // console.log($(".text"));
                for (var j = 0; j <= 12; j++) {
                    $(".text").eq(j).html(texts_12[j]);
                    $(".span3").eq(j).html(tgj_12[j]);
                    $(".span2").eq(j).html(yj_12[j]);
                }


                // 点击换页的1
                $(".page1").click(function() {
                    pages = 1;
                    console.log("booksShow2");
                    $("#Bookbox").html("");
                    layui.use('laytpl', function() {
                        var laytpl = layui.laytpl;
                        var data = {};
                        var getTpl = document.getElementById("page1").innerHTML;
                        // console.log(getTpl);
                        var view = document.getElementById("Bookbox");
                        // console.log(view);
                        laytpl(getTpl).render(data, function(html) {
                            // console.log(html);
                            view.innerHTML = html;

                            var booksstr = localStorage.getItem("books");
                            console.log(booksstr);
                            var booksjq = JSON.parse(booksstr);
                            console.log(booksjq);
                            // 描述
                            var texts_12 = [];
                            // 团购价
                            var tgj_12 = [];
                            // 原价
                            var yj_12 = [];
                            $.each(booksjq, function(v, i) {
                                console.log(i.desc);
                                texts_12.push(i.desc);
                                tgj_12.push(i.newPrice);
                                yj_12.push(i.oldPrice);
                            });
                            console.log(texts_12);
                            console.log(texts_12[0]);
                            // $(".text_1").html(texts_12[0]);
                            // $(".text_2").html(texts_12[1]);
                            console.log($(".text"));
                            for (var j = 0; j <= 12; j++) {
                                $(".text").eq(j).html(texts_12[j]);
                                $(".span3").eq(j).html(tgj_12[j]);
                                $(".span2").eq(j).html(yj_12[j]);
                            }
                            $(".page2").removeClass("layui-bg-green xuanzhong");
                            $(".page1").addClass("layui-bg-green xuanzhong");
                        });
                    });
                });
                // 点击换页的2
                $(".page2").click(function() {
                    pages = 2;
                    console.log("booksShow2");
                    $("#Bookbox").html("");
                    layui.use('laytpl', function() {
                        var laytpl = layui.laytpl;
                        var data = {};
                        var getTpl = document.getElementById("page2").innerHTML;
                        // console.log(getTpl);
                        var view = document.getElementById("Bookbox");
                        // console.log(view);
                        laytpl(getTpl).render(data, function(html) {
                            // console.log(html);
                            view.innerHTML = html;

                            var booksstr = localStorage.getItem("books");
                            console.log(booksstr);
                            var booksjq = JSON.parse(booksstr);
                            console.log(booksjq);
                            // 描述
                            var texts_12 = [];
                            // 团购价
                            var tgj_12 = [];
                            // 原价
                            var yj_12 = [];
                            $.each(booksjq, function(v, i) {
                                console.log(i.desc);
                                texts_12.push(i.desc);
                                tgj_12.push(i.newPrice);
                                yj_12.push(i.oldPrice);
                            });
                            console.log(texts_12);
                            console.log(texts_12[0]);
                            // $(".text_1").html(texts_12[0]);
                            // $(".text_2").html(texts_12[1]);
                            console.log($(".text"));
                            for (var j = 0; j <= 12; j++) {
                                $(".text").eq(j).html(texts_12[j]);
                                $(".span3").eq(j).html(tgj_12[j]);
                                $(".span2").eq(j).html(yj_12[j]);
                            }

                            $(".page1").removeClass("layui-bg-green xuanzhong");
                            $(".page2").addClass("layui-bg-green xuanzhong");
                        });
                    });
                });


                // 点击换页的上一页
                $(".prev").click(function() {
                    if (pages == 1) { //第一页的时候点击上一页
                        alert("当前处于首页");
                    } else { //第二页的时候点击上一页
                        pages = 1;
                        console.log("booksShow2");
                        $("#Bookbox").html("");
                        layui.use('laytpl', function() {
                            var laytpl = layui.laytpl;
                            var data = {};
                            var getTpl = document.getElementById("page1").innerHTML;
                            // console.log(getTpl);
                            var view = document.getElementById("Bookbox");
                            // console.log(view);
                            laytpl(getTpl).render(data, function(html) {
                                // console.log(html);
                                view.innerHTML = html;

                                var booksstr = localStorage.getItem("books");
                                console.log(booksstr);
                                var booksjq = JSON.parse(booksstr);
                                console.log(booksjq);
                                // 描述
                                var texts_12 = [];
                                // 团购价
                                var tgj_12 = [];
                                // 原价
                                var yj_12 = [];
                                $.each(booksjq, function(v, i) {
                                    console.log(i.desc);
                                    texts_12.push(i.desc);
                                    tgj_12.push(i.newPrice);
                                    yj_12.push(i.oldPrice);
                                });
                                console.log(texts_12);
                                console.log(texts_12[0]);
                                // $(".text_1").html(texts_12[0]);
                                // $(".text_2").html(texts_12[1]);
                                console.log($(".text"));
                                for (var j = 0; j <= 12; j++) {
                                    $(".text").eq(j).html(texts_12[j]);
                                    $(".span3").eq(j).html(tgj_12[j]);
                                    $(".span2").eq(j).html(yj_12[j]);
                                }
                                $(".page2").removeClass("layui-bg-green xuanzhong");
                                $(".page1").addClass("layui-bg-green xuanzhong");
                            });
                        });
                    }
                });

                // 点击换页的下一页
                $(".next").click(function() {
                    if (pages == 2) {
                        alert("当前处于尾页");
                    } else {
                        pages = 2;
                        console.log("booksShow2");
                        $("#Bookbox").html("");
                        layui.use('laytpl', function() {
                            var laytpl = layui.laytpl;
                            var data = {};
                            var getTpl = document.getElementById("page2").innerHTML;
                            // console.log(getTpl);
                            var view = document.getElementById("Bookbox");
                            // console.log(view);
                            laytpl(getTpl).render(data, function(html) {
                                // console.log(html);
                                view.innerHTML = html;

                                var booksstr = localStorage.getItem("books");
                                console.log(booksstr);
                                var booksjq = JSON.parse(booksstr);
                                console.log(booksjq);
                                // 描述
                                var texts_12 = [];
                                // 团购价
                                var tgj_12 = [];
                                // 原价
                                var yj_12 = [];
                                $.each(booksjq, function(v, i) {
                                    console.log(i.desc);
                                    texts_12.push(i.desc);
                                    tgj_12.push(i.newPrice);
                                    yj_12.push(i.oldPrice);
                                });
                                console.log(texts_12);
                                console.log(texts_12[0]);
                                // $(".text_1").html(texts_12[0]);
                                // $(".text_2").html(texts_12[1]);
                                console.log($(".text"));
                                for (var j = 0; j <= 12; j++) {
                                    $(".text").eq(j).html(texts_12[j]);
                                    $(".span3").eq(j).html(tgj_12[j]);
                                    $(".span2").eq(j).html(yj_12[j]);
                                }
                                $(".page1").removeClass("layui-bg-green xuanzhong");
                                $(".page2").addClass("layui-bg-green xuanzhong");
                            });
                        });
                    }
                });
            });
        });
    });
});